package com.yf.notify.utils;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class ArrayUtils {


    /**
     * 把一个数组切割成若干个子集合返回
     * @param arr 输入数组
     * @param num 每个子集合数量
     * @return
     */
    public static List<List<String>> split(String[] arr, int num) {

        // 切割成的数量
        int count = arr.length % num == 0 ? arr.length / num : arr.length / num + 1;

        List<List<String>> splitList = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            int index = i * num;
            List<String> list = new ArrayList<>();
            int j = 0;
            while (j < num && index < arr.length) {
                list.add(arr[index++]);
                j++;
            }

            splitList.add(list);
        }
        return splitList;
    }

    public static void main(String[] args) {
        String [] mobiles = new String[]{"18682216559", "18166052468", "19324858919"};
        System.out.println(JSON.toJSONString(split(mobiles, 2)));
    }
}
